﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using com.zhongjh.Library.Database1.Enum;

namespace com.zhongjh.Library.Database1
{
    /// <summary>
    /// 数据访问服务类
    /// </summary>
    public class CDbSvc : IDbSvc, IDisposable
    {
        #region 字段

        //资源是否释放
        private bool m_blnDisposed = false;

        private DbBase m_DbSvc = null;
        
        #endregion //字段

        #region 构造函数

        private CDbSvc()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="p_DbInfo"></param>
        public CDbSvc(ref CDbInfoEntity p_DbInfo)
        {
            GetDbConfig(ref p_DbInfo);
            CreateDbSvc(ref p_DbInfo);
        }

        #endregion //构造函数

        #region 公共方法

        /// <summary>
        /// 查询数据库数据(没有参数)
        /// </summary>
        /// <param name="p_strSql"></param>
        /// <param name="p_dtResult"></param>
        /// <returns></returns>
        public bool QueryDbDataNoParameter(string p_strSql, ref DataTable p_dtResult)
        {
            return m_DbSvc.QueryDbDataNoParameter(p_strSql, ref p_dtResult);
        }


        /// <summary>
        /// 修改数据库数据(没有参数)
        /// </summary>
        /// <param name="p_strSql"></param>
        /// <returns></returns>
        public bool ModifyDbDataNoParameter(string p_strSql)
        {
            return m_DbSvc.ModifyDbDataNoParameter(p_strSql);
        }


        /// <summary>
        /// 查询数据库数据(有参数)
        /// </summary>
        /// <param name="p_strSql"></param>
        /// <param name="p_DbParameter"></param>
        /// <param name="p_dtResult"></param>
        /// <returns></returns>
        public bool QueryDbDataHasParameter(string p_strSql, IDataParameter[] p_idpDbParameter, ref DataTable p_dtResult)
        {
            return m_DbSvc.QueryDbDataHasParameter(p_strSql, p_idpDbParameter, ref p_dtResult);
        }


        /// <summary>
        /// 修改数据库数据(有参数)
        /// </summary>
        /// <param name="p_strSql"></param>
        /// <param name="p_DbParameter"></param>
        /// <returns></returns>
        public bool ModifyDbDataHasParameter(string p_strSql, IDataParameter[] p_idpDbParameter)
        {
            return m_DbSvc.ModifyDbDataHasParameter(p_strSql, p_idpDbParameter);
        }


        /// <summary>
        /// 查询数据库数据(没有参数)(存储过程)
        /// </summary>
        /// <param name="p_strSql"></param>
        /// <param name="p_Result"></param>
        /// <returns></returns>
        public bool QueryDBDataNoParameter_SP(string p_strSql, ref DataTable p_dtResult)
        {
            return m_DbSvc.QueryDBDataNoParameter_SP(p_strSql, ref p_dtResult);
        }


        /// <summary>
        /// 修改数据库数据(没有参数)(存储过程)
        /// </summary>
        /// <param name="p_strSql"></param>
        /// <returns></returns>
        public bool ModifyDbDataNoParameter_SP(string p_strSql)
        {
            return m_DbSvc.ModifyDbDataNoParameter_SP(p_strSql);
        }


        /// <summary>
        /// 查询数据库数据(有参数)(存储过程)
        /// </summary>
        /// <param name="p_strSql"></param>
        /// <param name="p_DbParameter"></param>
        /// <param name="p_Result"></param>
        /// <returns></returns>
        public bool QueryDBDataHasParameter_SP(string p_strSql, IDataParameter[] p_idpDbParameter, ref DataTable p_dtResult)
        {
            return m_DbSvc.QueryDBDataHasParameter_SP(p_strSql, p_idpDbParameter, ref p_dtResult);
        }


        /// <summary>
        /// 修改数据库数据(有参数)(存储过程)
        /// </summary>
        /// <param name="p_strSql"></param>
        /// <param name="p_DbParameter"></param>
        /// <returns></returns>
        public bool ModifyDbDataHasParameter_SP(string p_strSql, IDataParameter[] p_idpDbParameter)
        {
            return m_DbSvc.ModifyDbDataHasParameter_SP(p_strSql, p_idpDbParameter);
        }


        /// <summary>
        /// 批量保存
        /// </summary>
        /// <param name="p_dsData"></param>
        /// <returns></returns>
        public bool BanthSave(DataSet p_dsData)
        {
            return m_DbSvc.BanthSave(p_dsData);
        }


        #region 删除代码

        ///// <summary>
        ///// 批量保存
        ///// </summary>
        ///// <param name="p_dtData"></param>
        ///// <returns></returns>
        //public bool BatchSave(ref DataTable p_dtData)
        //{
        //    bool result = false;
        //    DataTable dtAdd = null;
        //    DataTable dtDelete = null;
        //    DataTable dtModify = null;

        //    try
        //    {
        //        BeginTransaction();

        //        dtAdd = p_dtData.GetChanges(DataRowState.Added);
        //        dtDelete = p_dtData.GetChanges(DataRowState.Deleted);
        //        dtModify = p_dtData.GetChanges(DataRowState.Modified);

        //        result = BatchAdd(ref dtAdd);
        //        if (result == true)
        //            result = BatchDelete(ref dtDelete);
        //        if (result == true)
        //            result = BatchModify(ref dtModify);

        //        if (result == true)
        //            p_dtData.AcceptChanges();

        //        CommitTransaction();
        //    }
        //    catch (Exception ex)
        //    {
        //        RollbackTransaction();
        //        result = false;
        //    }

        //    return result;
        //}

        ///// <summary>
        ///// 批量添加
        ///// </summary>
        ///// <param name="p_dtData"></param>
        ///// <returns></returns>
        //public bool BatchAdd(ref DataTable p_dtData)
        //{
        //    bool result = false;
        //    string strInsert = string.Empty;

        //    try
        //    {
        //        if (p_dtData.PrimaryKey.Length > 0)
        //        {
        //            DataColumn dcTemp = null;
        //            DataRow drTemp = null;
        //            int intColumnCount = 0;
        //            int intRowCount = 0;
        //            int intAidCount = 0;
        //            StringBuilder sbTemp1 = new StringBuilder();
        //            StringBuilder sbTemp2 = new StringBuilder();
        //            DataColumn[] dcPrimary = null;
        //            bool blnIsPrimary = false;

        //            sbTemp1.Append("insert into " + p_dtData.TableName + "(");
        //            sbTemp2.Append(") values (");
        //            intColumnCount = p_dtData.Columns.Count;
        //            dcPrimary = p_dtData.PrimaryKey;
        //            for (int i1 = 0; i1 < intColumnCount; i1++)
        //            {
        //                dcTemp = p_dtData.Columns[i1];
        //                blnIsPrimary = false;
        //                for (int i2 = 0; i2 < dcPrimary.Length; i2++)
        //                {
        //                    if (dcTemp.ColumnName == dcPrimary[i2].ColumnName)
        //                    {
        //                        blnIsPrimary = true;
        //                        break;
        //                    }
        //                }

        //                if (blnIsPrimary == false)
        //                {
        //                    sbTemp1.Append(dcTemp.ColumnName);
        //                    sbTemp2.Append("@" + dcTemp.ColumnName);
        //                    if (i1 < intColumnCount - 1 - intAidCount)
        //                    {
        //                        sbTemp1.Append(", ");
        //                        sbTemp2.Append(", ");
        //                    }
        //                    else
        //                    {
        //                        intAidCount++;
        //                    }
        //                }
        //            }
        //            sbTemp2.Append(")");
        //            strInsert = sbTemp1.ToString() + sbTemp2.ToString();

        //            BeginTransaction();

        //            IDataParameter[] idpDbPara = null;
        //            intRowCount = p_dtData.Rows.Count;
        //            for (int i1 = 0; i1 < intRowCount; i1++)
        //            {
        //                drTemp = p_dtData.Rows[i1];
        //                idpDbPara = m_DbSvc.CreateDataParameter(intColumnCount - intAidCount);
        //                for (int i2 = 0; i2 < intColumnCount; i2++)
        //                {
        //                    dcTemp = p_dtData.Columns[i2];
        //                    blnIsPrimary = false;
        //                    for (int i3 = 0; i3 < dcPrimary.Length; i3++)
        //                    {
        //                        if (dcTemp.ColumnName == dcPrimary[i3].ColumnName)
        //                        {
        //                            blnIsPrimary = true;
        //                            break;
        //                        }
        //                    }

        //                    if (blnIsPrimary == false)
        //                    {
        //                        idpDbPara[i2].ParameterName = "@" + dcTemp.ColumnName;
        //                        idpDbPara[i2].Value = drTemp[i1];
        //                    }
        //                }
        //                result = ModifyDbDataHasParameter(strInsert, idpDbPara);
        //                if (result == false)
        //                    break;
        //            }

        //            if (result == true)
        //                CommitTransaction();
        //            else
        //                RollbackTransaction();
        //        }
        //        else
        //        {
        //            result = false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        RollbackTransaction();
        //        result = false;
        //    }

        //    return result;
        //}

        ///// <summary>
        ///// 批量修改
        ///// </summary>
        ///// <param name="p_dtData"></param>
        ///// <returns></returns>
        //public bool BatchModify(ref DataTable p_dtData)
        //{
        //    bool result = false;
        //    string strUpdate = string.Empty;
        //    IDataParameter[] idpDbPare = null;

        //    try
        //    {
        //        if (p_dtData.PrimaryKey.Length > 0)
        //        {
        //            DataColumn[] dcPrimary = null;
        //            bool blnIsPrimary = false;
        //            int intPrimaryCount = 0;

        //            strUpdate = "update " + p_dtData.TableName + " set (";
        //            dcPrimary = p_dtData.PrimaryKey;

        //            for (int i1 = 0; i1 < p_dtData.Columns.Count; i1++)
        //            {
        //                blnIsPrimary = false;
        //                for (int i2 = 0; i2 < dcPrimary.Length; i2++)
        //                {
        //                    if (p_dtData.Columns[i1].ColumnName == dcPrimary[i2].ColumnName)
        //                    {
        //                        blnIsPrimary = true;
        //                        intPrimaryCount++;
        //                        break;
        //                    }
        //                }

        //                if (blnIsPrimary == false)
        //                {
        //                    strUpdate = strUpdate + p_dtData.Columns[i1].ColumnName + "=" + m_strAt + p_dtData.Columns[i1].ColumnName;
        //                    if (i1 < (p_dtData.Columns.Count - 1 - intPrimaryCount))
        //                    {
        //                        strUpdate = strUpdate + ", ";
        //                    }
        //                }
        //            }

        //            strUpdate = strUpdate + ") where ";

        //            for (int i1 = 0; i1 < dcPrimary.Length; i1++)
        //            {
        //                strUpdate = strUpdate + dcPrimary[i1].ColumnName + "=" + m_strAt + dcPrimary[i1].ColumnName;
        //                if (i1 < dcPrimary.Length - 1)
        //                    strUpdate = strUpdate + ", ";
        //            }

        //            /////////////////////////////////////////////////////////////////////////////////////////////

        //            DataRow drTmep = null;

        //            blnIsPrimary = false;
        //            intPrimaryCount = 0;
        //            for (int i1 = 0; i1 < p_dtData.Rows.Count; i1++)
        //            {
        //                drTmep = p_dtData.Rows[i1];
        //                idpDbPare = CreateDataParameter(p_dtData.Rows.Count);

        //                for (int i2 = 0; i2 < p_dtData.Columns.Count; i2++)
        //                {
        //                    blnIsPrimary = false;
        //                    for (int i3 = 0; i3 < dcPrimary.Length; i3++)
        //                    {
        //                        if (p_dtData.Columns[i2].ColumnName == dcPrimary[i3].ColumnName)
        //                        {
        //                            blnIsPrimary = true;
        //                            intPrimaryCount++;
        //                            break;
        //                        }
        //                    }

        //                    if (blnIsPrimary == false)
        //                    {
        //                        idpDbPare[i1 - intPrimaryCount].ParameterName = m_strAt + p_dtData.Columns[i2].ColumnName;
        //                        idpDbPare[i1 - intPrimaryCount].Value = drTmep[i1];
        //                    }
        //                    else
        //                    {
        //                        idpDbPare[p_dtData.Columns.Count - dcPrimary.Length + intPrimaryCount].ParameterName = m_strAt + p_dtData.Columns[i2].ColumnName;
        //                        idpDbPare[p_dtData.Columns.Count - dcPrimary.Length + intPrimaryCount].Value = drTmep[i1];
        //                    }
        //                }
        //            }

        //            BeginTransaction();

        //            CommitTransaction();
        //        }
        //        else
        //        {
        //            result = false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        RollbackTransaction();
        //        result = false;
        //    }

        //    return result;
        //}

        ///// <summary>
        ///// 批量修改
        ///// </summary>
        ///// <param name="p_dtData"></param>
        ///// <returns></returns>
        //public bool BatchDelete(ref DataTable p_dtData)
        //{
        //    bool result = false;
        //    string strDelete = string.Empty;
        //    IDataParameter[] idpDbPare = null;

        //    try
        //    {
        //        if (p_dtData.PrimaryKey.Length > 0)
        //        {
        //            DataColumn[] dcPrimary = null;

        //            strDelete = "delete from " + p_dtData.TableName + " where ";
        //            dcPrimary = p_dtData.PrimaryKey;

        //            for (int i1 = 0; i1 < dcPrimary.Length; i1++)
        //            {
        //                strDelete = strDelete + dcPrimary[i1].ColumnName + @"=@" + dcPrimary[i1].ColumnName;
        //                if (i1 < dcPrimary.Length - 1)
        //                    strDelete = strDelete + ", ";
        //            }

        //            /////////////////////////////////////////////////////////////////////////////////////////////

        //            DataRow drTemp = null;

        //            for (int i1 = 0; i1 < p_dtData.Rows.Count; i1++)
        //            {
        //                drTemp = p_dtData.Rows[i1];
        //                idpDbPare = CreateDataParameter(dcPrimary.Length);

        //                for (int i2 = 0; i2 < dcPrimary.Length; i2++)
        //                {
        //                    idpDbPare[i2].ParameterName = m_strAt + dcPrimary[i2].ColumnName;
        //                    idpDbPare[i2].Value = drTemp[dcPrimary[i2].ColumnName];
        //                }
        //                result = ModifyDbDataHasParameter(strDelete, idpDbPare);

        //                if (result == false)
        //                    break;
        //            }

        //            if (result == true)
        //                CommitTransaction();
        //            else
        //                RollbackTransaction();
        //        }
        //        else
        //        {
        //            result = false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        RollbackTransaction();
        //        result = false;
        //    }

        //    return result;
        //}

        #endregion //删除代码

        /// <summary>
        /// 开始事务
        /// </summary>
        public void BeginTransaction()
        {
            m_DbSvc.BeginTransaction();
        }

        /// <summary>
        /// 提交事务
        /// </summary>
        public void CommitTransaction()
        {
            m_DbSvc.CommitTransaction();
        }

        /// <summary>
        /// 回滚事务
        /// </summary>
        public void RollbackTransaction()
        {
            m_DbSvc.RollbackTransaction();
        }

        /// <summary>
        /// 关闭连接
        /// </summary>
        public void CloseConnection()
        {
            m_DbSvc.CloseConnection();
        }

        /// <summary>
        /// 创建数据参数
        /// </summary>
        /// <param name="p_intLength"></param>
        /// <returns></returns>
        public IDataParameter[] CreateDataParameter(int p_intLength)
        {
            return m_DbSvc.CreateDataParameter(p_intLength);
        }

        /// <summary>
        /// 获取序号
        /// </summary>
        /// <param name="p_lngCode"></param>
        /// <returns></returns>
        public long GetSN(long p_lngCode)
        {
            return m_DbSvc.GetSN(p_lngCode);
        }

        /// <summary>
        /// 回收序号
        /// </summary>
        /// <param name="p_lngCode"></param>
        /// <param name="p_lngSn"></param>
        /// <returns></returns>
        public bool RecSN(long p_lngCode, long p_lngSn)
        {
            return m_DbSvc.RecSN(p_lngCode, p_lngSn);
        }

        #endregion //公共方法

        #region 私有方法

        /// <summary>
        /// 创建数据库访问服务对象
        /// </summary>
        /// <param name="p_DbInfo"></param>
        private void CreateDbSvc(ref CDbInfoEntity p_DbInfo)
        {
            EDbType m_DbType = p_DbInfo.DbType;
            EDbDrive m_DbDrive = p_DbInfo.DbDrive;

            switch (m_DbType)
            {
                case EDbType.Access:
                    m_DbSvc = new OleDb(p_DbInfo);
                    break;
                case EDbType.Sql:
                    switch (m_DbDrive)
                    {
                        case EDbDrive.Sql:
                            m_DbSvc = new SqlDb(p_DbInfo);
                            break;
                        case EDbDrive.OleDb:
                            m_DbSvc = new OleDb(p_DbInfo);
                            break;
                        default:
                            m_DbSvc = null;
                            break;
                    }
                    break;
                case EDbType.MySql:
                    switch (m_DbDrive)
                    {
                        case EDbDrive.MySql:
                            m_DbSvc = new MySqlDb(p_DbInfo);
                            break;
                        case EDbDrive.OleDb:
                            m_DbSvc = new OleDb(p_DbInfo);
                            break;
                        default:
                            m_DbSvc = null;
                            break;
                    }
                    break;
                case EDbType.Oracle:
                    switch (m_DbDrive)
                    {
                        case EDbDrive.Oracle:
                            m_DbSvc = new OracleDb(p_DbInfo);
                            break;
                        case EDbDrive.OleDb:
                            m_DbSvc = new OleDb(p_DbInfo);
                            break;
                        default:
                            m_DbSvc = null;
                            break;
                    }
                    break;
                default:
                    m_DbSvc = null;
                    break;
            }
        }

        /// <summary>
        /// 获取数据库配置信息
        /// </summary>
        /// <param name="p_DbInfo"></param>
        private void GetDbConfig(ref CDbInfoEntity p_DbInfo)
        {
            if (p_DbInfo.IsReadingConfigFild == true)
            {
                DbConfig dbConfig = null;

                if (p_DbInfo.ConfigFildPath.Equals(string.Empty))
                {
                    dbConfig = new DbConfig(InnerCommon.m_strDbConfigPath);
                }
                else
                {
                    dbConfig = new DbConfig(p_DbInfo.ConfigFildPath);
                }
                p_DbInfo = dbConfig.GetDbInfo(p_DbInfo.Id);
            }
        }

        #endregion //私有方法

        #region 释放资源

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool p_blnDisposing)
        {
            if (!this.m_blnDisposed)
            {
                if (p_blnDisposing)
                {
                    //释放托管资源
                    m_DbSvc.Dispose();
                }
                //释放非托管资源

            }
            m_blnDisposed = true;
        }

        ~CDbSvc()
        {
            Dispose(false);
        }

        #endregion //释放资源
    }
}
